#!/bin/bash
###
 # @Author: dvlproad
 # @Date: 2023-02-27 19:02:53
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-02-28 15:38:48
 # @Description: 
### 
# sh check_branch_must_contain.sh ${Develop_Branchs_FILE_PATH}
:<<!
检查本分支有没有合入必须合入的分支
!
#responseResult=$(git log --graph --pretty=format:'%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --after "10-12-2022" --merges )
#responseResult=$(git log --pretty=format:'%s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --after "10-12-2022" --merges)

#responseResult=$(git log --pretty=format:'%C(yellow)%H' --after "10-12-2022" --no-merges )

#git log --pretty=format:'-%C(yellow)%d%Creset %s' --after "2022-10-16"

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}

JQ_EXEC=`which jq`

CurrentDirName=${PWD##*/}

echo "$0检查本分支有没有合入必须合入的分支"
## shell 参数具名化(-mustContain 和 -messageIfError 等可能为空，所以先不使用具名)
#show_usage="args: [-appInfoF, -curBranch , -mustContain , -messageIfError]\
#                                  [--app-info-json-file=, --current-branchName=, --mustContain-branchNameArray=, --extraMessage-ifError=]"
#
#while [ -n "$1" ]
#do
#        case "$1" in
#                -appInfoF|--app-info-json-file) Develop_Branchs_FILE_PATH=$2; shift 2;;
#                -curBranch|--current-branchName) currentBranch=$2; shift 2;;
#                -mustContain|--mustContain-branchNameArray) mustContainBranchNameArray=$2; shift 2;;
#                -messageIfError|--extraMessage-ifError) ExtraMessageIfError=$2; shift 2;;
#                --) break ;;
#                *) echo $1,$2,$show_usage; break ;;
#        esac
#done

Develop_Branchs_FILE_PATH=$1
currentBranch=$2
mustContainBranchNameArray=($3) # 外部已把数组换成了字符串
ExtraMessageIfError=$4
#echo "mustContainBranchNameArray=${mustContainBranchNameArray[*]}"
#echo "ExtraMessageIfError=${ExtraMessageIfError}"


# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
#echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_json_file_check_script_path="$(sh ${qscript_path_get_filepath} qbase json_file_check)"

source ${bulidScriptCommon_dir_Absolute}/a_function.sh ${bulidScriptCommon_dir_Absolute} # 为了使用 updatePackageErrorCodeAndMessage
sh ${qbase_json_file_check_script_path} -checkedJsonF "${Develop_Branchs_FILE_PATH}"
if [ $? != 0 ]; then
    exit 1
fi

allContainBranchArray=$(cat "${Develop_Branchs_FILE_PATH}" | ${JQ_EXEC} '.package_merger_branchs') # -r 去除字符串引号
#echo "allContainBranchArray=${allContainBranchArray}"
if [ -z "${allContainBranchArray}" ] || [ "${allContainBranchArray}" == "null" ]; then
    allContainBranchArray="" # null" 也设置成空字符串
    echo "❌Error:未找到当前分支合入的其他分支信息，将退出allContainBranchArray=${allContainBranchArray}"
    exit_script
fi

allContainBranchNames=$(cat "${Develop_Branchs_FILE_PATH}" | ${JQ_EXEC} '.package_merger_branchs' | ${JQ_EXEC} -r '.[].name') # -r 去除字符串引号
allContainBranchNames=($allContainBranchNames) # 此时才是真正的数组
echo "====要检查本分支是否包含以下分支allContainBranchNames=${allContainBranchNames}\n"


echo "\n\n\n"
echo "\n------检查本分支有没有必须要合入的分支------"
mustContainBranchNameCount=${#mustContainBranchNameArray[@]}
echo "mustContainBranchNameCount=${mustContainBranchNameCount}"
if [ ${mustContainBranchNameCount} == 0 ]; then
    echo "本分支没有必须要合入的分支的检查"
else
    echo "本分支有必须要合入的分支的检查,下面开始检查=============="
    for ((i=0;i<mustContainBranchNameCount;i++))
    do
        mustContainBranchName=${mustContainBranchNameArray[i]}
        if [[ "${allContainBranchNames[*]}" =~ ${mustContainBranchName} ]]; then
            echo "本分支有包含必须合入的${mustContainBranchName}分支"
        else
            PackageErrorCode=-1
            PackageErrorMessage="本分支${currentBranch}不能缺少${mustContainBranchName}分支上的代码，请检查是否合入且全部合入"
            if [ -n "${ExtraMessageIfError}" ]; then
                PackageErrorMessage+="${ExtraMessageIfError}"
            fi
            updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
            exit_script
        fi
    done
fi


